package com.hfut.eeg.common.util;

import com.hfut.eeg.dao.BrainFFTMapper;
import com.hfut.eeg.dao.EEGGuideMapper;
import com.hfut.eeg.dao.EEGMapper;
import com.hfut.eeg.dto.EEG;
import com.hfut.eeg.dto.EEGTrans;
import com.hfut.eeg.dto.PatientListResult;
import com.hfut.eeg.dto.ReportInfo;
import com.hfut.eeg.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.common.util
 * @Description: 获取当前系统时间，并生成固定时间格式
 * @date 2019-11-06 14:39
 */
@Component
@Scope("singleton")
public class GenerateCommon {

    @Autowired
    private PatientInfoService patientInfoService;

    @Autowired
    private ReportService reportService;

    @Autowired
    private EEGService eegService;
    @Autowired
    private EEGMapper eegMapper;
    @Autowired
    private EEGGuideMapper eegGuideMapper;
    @Autowired
    private BrainFFTMapper brainFFTMapper;

    @Autowired
    private EEGGuideService eegGuideService;
    @Autowired
    private BrainFFTService brainFFTService;
    @Autowired
    private BrainEnergyService brainEnergyService;
    @Autowired
    private BraninChannelService braninChannelService;

    private static String storeDate = "19971010";
    private static int count = 0;

    /**
    * 生成当前系统时间的date格式
    */
    public Date getSysDate(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date result = null;
        try{
            result = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
    * 根据规则生成id属性
    */
    public  String generateID(GenerateID generateID){
        count++;
        if(generateID instanceof SetupService)
            return "DH"+storeDate+String.format("%3d", count).replace(" ", "0");
        else
            return "BH"+storeDate+String.format("%3d", count).replace(" ", "0");
    }


    /**
    * 在Spring装载bean时执行，初始化当前最新的主键
    */
    public  void initID(GenerateID generateID){
        //System.out.println(patientInfoService.getPatientList(1,1).get(0).getCheckTime().toString());
        String ID = generateID.getLatestID();

        String date = ID.substring(2,10);
        String start = ID.substring(10);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String nowDate = df.format(new Date());
        storeDate = nowDate;
        if (nowDate.compareTo(date) != 0){
            count = 0;
        }else {
            count = Integer.parseInt(start);
        }
    }

    public void generteReport() throws ParseException {
        List<PatientListResult> patientListResults = patientInfoService.getAllPatients();
        List<ReportInfo> reportInfos = new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (PatientListResult patientListResult : patientListResults){
            ReportInfo reportInfo = new ReportInfo();
            reportInfo.setPatientId(patientListResult.getPatientId());
            reportInfo.setTime(new Date());
            reportInfo.setReportDate(new Date());
            reportInfo.setDoctorId("DH20191112001");
            reportInfos.add(reportInfo);
        }
        reportService.insertReportList(reportInfos);
    }

    /**
    * @Description: EEG数据模拟，用于在初始时数据未加载，预先存储部分数据
    * @param null
    * @return
    * @author 梁琦
    * @date 2019-12-30 20:03
    * @throws
    */
    public void stimulateEEG(String patientId,int order,int num) {
        HashMap<String,Object> getReq = new HashMap<>();
        getReq.put("id","BH20100101009");
        getReq.put("patientId","BH20100101009");
        getReq.put("eegId","BH20100101009");
        ArrayList<Integer> list = new ArrayList<>();
        for (int i=order;i<order+num;i++){
            list.add(i);
        }
        getReq.put("sequence",list);

        //eeg数据拷贝与存储
        List<EEG> eegs = eegMapper.getDat(getReq);
        for (int i=0;i<eegs.size();i++){
            ArrayList<ArrayList> lists = BlobListTrans.transEEGTransToList(eegs.get(i));
            eegService.insertEEGData(lists,patientId);
        }
        //均导数据拷贝与存储
        List<EEGTrans> allGuideBySeg = eegGuideMapper.getAllGuideBySeg(getReq);
        for (int i=0;i<allGuideBySeg.size();i++){
            ArrayList<ArrayList> lists = BlobListTrans.transEEGTransToList(allGuideBySeg.get(i));
            eegGuideService.insertAllGuide(lists,patientId);
        }
        //单导数据拷贝与存储
        List<EEGTrans> singleGuideBySeg = eegGuideMapper.getSingleGuideBySeg(getReq);
        for (int i=0;i<singleGuideBySeg.size();i++){
            ArrayList<ArrayList> lists = BlobListTrans.transEEGTransToList(singleGuideBySeg.get(i));
            eegGuideService.insertSingleGuide(lists,patientId);
        }
        //FFT数据拷贝与存储
        List<EEGTrans> brainFFT = brainFFTMapper.getBrainFFT(getReq);
        for (int i=0;i<brainFFT.size();i++){
            ArrayList<ArrayList> lists = BlobListTrans.transEEGTransToList(brainFFT.get(i));
            brainFFTService.insertFFTByArray(lists,patientId);
        }

        //脑部通道数据拷贝与存储
        ArrayList<ArrayList> channel = new ArrayList<>();
        for (int i=0;i<16;i++){
            ArrayList tmp = new ArrayList();
            for (int j=0;j<num;j++){
                tmp.add(0);
            }
            channel.add(tmp);
        }
        braninChannelService.insertBrainChannelByArr(channel,patientId);

        //脑补能量数据拷贝与存储
        ArrayList<ArrayList> energe = new ArrayList<>();
        for (int i=0;i<5;i++){
            ArrayList tmp1 = new ArrayList();
            for (int j=0;j<num;j++){
                tmp1.add(0.1*j);
            }
            energe.add(tmp1);
        }
        brainEnergyService.insertBrainEnergyByArr(energe,patientId);
    }
}
